//
// Created by 谢祥平 on 22-10-3.
//

#ifndef CARP_SEQUENCESTACK_H
#define CARP_SEQUENCESTACK_H

#include "head.h"
#define MAXSIZE 10

typedef struct {
    int data[MAXSIZE];
    int top;
} sqStack;

typedef struct linkNode {
    double date;
    struct linkNode *next;
} *liStack;

void initStack(sqStack &S) {
    S.top = -1;
}

bool initStack(liStack &S) {
    S = (liStack) malloc(sizeof(linkNode));
    if(S == NULL)
        return false;
    else return true;
}

bool stackEmpty(sqStack S) {
    return (S.top == -1);
}

bool stackEmpty(liStack S) {
    return (S->next == NULL);
}

bool push(sqStack &S, int x) {
    if(S.top >= MAXSIZE - 1) {
        return false;
    }

    S.data[++S.top] = x;
    return true;
}

bool push(liStack &S, int x) {
    linkNode *s = (linkNode *) malloc(sizeof(linkNode));
    if(s == NULL)
        return false;

    if(S->next != NULL) {
        s->date = x;
        s->next = S->next;
        S->next = s;
    } else {
        s->date = x;
        s->next = S->next;
        S->next = s;
    }
    return true;
}

bool pop(sqStack &S, int &x) {
    if(S.top <= -1) {
        return false;
    }

    x = S.data[S.top--];
    return true;
}

bool pop(liStack &S, int &x) {
    if(S->next == NULL) {
        return false;
    }

    linkNode *p = S->next;
    x = p->date;
    S->next = S->next->next;
    free(p);
    return true;
}

bool getTop(sqStack &S, int &x) {
    if(S.top <= -1) {
        return false;
    }

    x = S.data[S.top];
    return true;
}

bool getTop(liStack &S, int &x) {
    if(S->next == NULL) {
        return false;
    }

    x = S->next->date;
    return true;
}


#endif //CARP_SEQUENCESTACK_H